package com.xckj.teacher.framework;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.gyf.immersionbar.ImmersionBar;
import com.xckj.teacher.R;
import com.xckj.teacher.view.LoadingDialog;

/**
 * @Author: ChenYao
 * @Describe: Activity的基类
 * @Date: 2024/4/10 21:05
 */
public abstract class BaseActivity<T extends ViewDataBinding> extends AppCompatActivity implements IBaseView {
    protected T dataBinding;
    private ImmersionBar mImmersionBar;
    private Activity activity;
    private LoadingDialog loadingDialog;
    private int dialogCount = 0;

    @SuppressLint("SourceLockedOrientationActivity")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(loadLayout());
        initStatusBar();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        activity = this;
        dataBinding = DataBindingUtil.setContentView(this, loadLayout());
        initData();
        setListener();
    }

    /**
     * 加载布局
     *
     * @return 布局Id
     */
    abstract protected int loadLayout();

    /**
     * 初始化数据
     */
    abstract protected void initData();

    /**
     * 设置监听
     */
    abstract protected void setListener();

    /**
     * 初始化沉浸式状态栏
     */
    private void initStatusBar() {
        if (isStatusBarEnabled()) {
            getStatusBarConfig().init();
        }
    }

    /**
     * 是否使用沉浸式状态栏
     *
     * @return true or false
     */
    protected boolean isStatusBarEnabled() {
        return true;
    }

    /**
     * 状态栏字体是否深色模式
     *
     * @return true or false
     */
    protected boolean isStatusBarDarkFont() {
        return true;
    }

    /**
     * 设置状态栏颜色
     */
    protected int setStatusBarColor() {
        return R.color.transparent;
    }

    /**
     * 初始化沉浸式状态栏
     *
     * @return ImmersionBar
     */
    protected ImmersionBar createStatusBarConfig() {
        return ImmersionBar
                .with(this)
                .statusBarDarkFont(isStatusBarDarkFont())
                .statusBarColor(setStatusBarColor())
                .fitsSystemWindows(true)
                .keyboardEnable(false)
                .navigationBarColor(R.color.white)
                .autoDarkModeEnable(true)
                .autoNavigationBarDarkModeEnable(true);
    }

    /**
     * 获取状态栏沉浸的配置对象
     *
     * @return ImmersionBar
     */
    public ImmersionBar getStatusBarConfig() {
        if (null == mImmersionBar) {
            mImmersionBar = createStatusBarConfig();
        }
        return mImmersionBar;
    }

    @Override
    public void showMessage(String message) {
        if (!TextUtils.isEmpty(message)) {
            runOnUiThread(() -> Toast.makeText(activity, message, Toast.LENGTH_SHORT).show());
        }
    }
    @Override
    public void showLoading() {
        runOnUiThread(() -> {
            dialogCount++;
            if (null == loadingDialog || !loadingDialog.isShowing()) {
                loadingDialog = new LoadingDialog(this);
                loadingDialog.create();
            }
            if (!loadingDialog.isShowing()) {
                loadingDialog.show();
            }
        });
    }

    /**
     * 隐藏Loading
     */
    @Override
    public void hideLoading() {
        runOnUiThread(() -> {
            dialogCount--;
            if (null == loadingDialog) {
                return;
            }
            if (dialogCount <= 0) {
                loadingDialog.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }
}
